//
//  OERechargeViewController.m
//  OneEightyNine
//
//  Created by Mac on 2019/3/4.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OERechargeViewController.h"

@interface OERechargeViewController ()
@property(nonatomic, strong) NSMutableArray                     *buttonArray;/**<  */
@property(nonatomic, strong) UITextField                     *textFiled;/**< <##> */
@property(nonatomic, strong) NSArray                     *dataSouce;/**< <##> */
@end

@implementation OERechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = OESTRING(@"RECHARGE DEPOSIT BALANCE");
    [self configSubView];
    [self addNotifacations];
    [OEUserDataController sendGetPayMethod:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            NSArray * array = (id)resp;
            NSMutableArray * dataSouce = NSMutableArray.new;
            for (NSDictionary * dic in array) {
                NSString * pay_name = [dic[@"pay_name"] description];
                NSString * pay_id  = [dic[@"pay_id"] description];
//                NSString * pay_code;
                BOOL selected = NO;
                if ([array indexOfObject:dic] == 0) {
                    selected = YES;
                }
                
                STTableEasyModel * model = [[STTableEasyModel alloc] initWithTextString:pay_name accessoryView:[self buttonWithSelcted:selected]];
                model.isSlected = selected;
                [dataSouce addObject:model];
                model.obj_id = pay_id;
            }
            self.dataSouce = dataSouce.copy;
            [self.tableView reloadData];
        }
    }];
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"PAYPAL") accessoryView:[self buttonWithSelcted:YES]],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"CREDIT CARD") accessoryView:[self buttonWithSelcted:NO]],
                       [[STTableEasyModel alloc] initWithTextString:OESTRING(@"BANK DEPOSIT") accessoryView:[self buttonWithSelcted:NO]]
                       ];
    // Do any additional setup after loading the view.
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(st_notifactionMethod:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)st_notifactionMethod:(NSNotification*)notifacation{
    if (notifacation.object == self.textFiled) {
        for (UIButton * button in self.buttonArray) {
            button.selected = NO;
            button.backgroundColor = UIColor.whiteColor;
        }
        [self.textFiled st_setBorderWith:0.5 borderColor:OE_LIMITE_COLOR cornerRadius:2];
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --configSubView
- (STButton*)buttonWithSelcted:(BOOL)selected{
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"36×36未选中"]];
    buyButton.userInteractionEnabled = NO;
    [buyButton setImage:[UIImage imageNamed:@"34×34选中"] forState:UIControlStateSelected];
    buyButton.selected = selected;
    return buyButton;
}

- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    
    NSArray * array = @[@"10Dollar",@"20Dollar",@"50Dollar",@"100Dollar",@"200Dollar"];
    __weak typeof(self) weakSelf =  self;
    NSInteger count = array.count;
    CGFloat buttonWith = (UIScreenWidth-4 * self.insetX) / 3;
    CGFloat inset = self.insetX;
    CGFloat top = inset;
    self.buttonArray = NSMutableArray.new;
    for (NSInteger i = 0 ;i < array.count;i ++) {
        NSString * title = array[i];
        STButton * button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, 60)
                                                      title:title
                                                 titleColor:UIColor.blackColor
                                                  titleFont:18
                                               cornerRadius:2
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:nil];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [button setTitleColor:OE_LIMITE_COLOR forState:UIControlStateSelected];
        [button st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:2];
        [button setClicAction:^(UIButton *sender) {
            [weakSelf onslectedMoneyButton:sender];
        }];
        [header addSubview:button];
        [self.buttonArray addObject:button];
        button.left = inset +  inset * (i % 3) + buttonWith  * (i % 3);
        button.top =  inset +  inset * (i / 3) + 60  * (i / 3);
        if (i == 0) {
            [self onslectedMoneyButton:button];
        }
    }
    
    self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, buttonWith, 60)];
    [self.textFiled st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:2];
    self.textFiled.left = inset +  inset * (array.count % 3) + buttonWith  * (array.count % 3);
    self.textFiled.top =  inset +  inset * (array.count / 3) + 60  * (array.count / 3);
    self.textFiled.textAlignment = NSTextAlignmentCenter;
    self.textFiled.placeholder = OESTRING(@"OTHER");
    self.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:self.textFiled];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, self.textFiled.bottom + inset, UIScreenWidth, 10)];
    line.backgroundColor = OE_GrayColor;
    [header addSubview:line];
    
    STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, line.bottom, UIScreenWidth - 20, 44)
                                                    text:@"Payment method"
                                               textColor:SecendTextColor
                                                    font:16
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLabel];
    header.height = titleLabel.bottom ;
    self.tableView.tableHeaderView = header;
    
    STButton * payButton = [[STButton alloc] initWithFrame:CGRectMake(40, 0, UIScreenWidth - 80, 44)
                                                     title:OESTRING(@"PAYMENT")
                                                titleColor:UIColor.whiteColor
                                                 titleFont:15
                                              cornerRadius:22
                                           backgroundColor:OE_LIMITE_COLOR
                                           backgroundImage:nil
                                                     image:nil];
    payButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    payButton.top = TMUtils.tabBarTop - 40 - 44;
    [payButton makeImageRight];
    [self.view addSubview:payButton];
    [payButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedPayButton];
    }];
    
    STButton * alertbutton = [[STButton alloc] initWithFrame:CGRectMake(10, payButton.bottom + 10, UIScreenWidth - 20, 44)
                                                  title:OESTRING(@"Click  confim the rechage,that is,have argeeed the <recharge argemnt>")
                                             titleColor:SecendTextColor
                                              titleFont:13
                                           cornerRadius:0
                                        backgroundColor:nil
                                        backgroundImage:nil
                                                  image:nil];
    alertbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    alertbutton.titleLabel.textAlignment = NSTextAlignmentCenter;
    alertbutton.titleLabel.numberOfLines = 0;
    [self.view addSubview:alertbutton];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake(0, TMUtils.navgationBarBootom, UIScreenWidth, payButton.top - TMUtils.navgationBarBootom);
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    for (STTableEasyModel * emodel in self.dataSouce) {
        UIButton * button = (id)emodel.accessoryView;
        button.selected = NO;
        emodel.isSlected = NO;
    }
    UIButton * button = (id)model.accessoryView;
    button.selected = YES;
    model.isSlected = YES;
    [self.tableView reloadData];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (NSString*)currentSelctedItemMoney{
    for (UIButton * button in self.buttonArray) {
        if (button.selected) {
            NSString * title = [button.currentTitle stringByReplacingOccurrencesOfString:@"Dollar" withString:@""];
            return title;
        }
    }
    return self.textFiled.text;
}
#pragma mark --Action Method
- (void)onslectedMoneyButton:(UIButton*)sender{
    [self.textFiled st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:2];
    [self.textFiled resignFirstResponder];
    
    for (UIButton * button in self.buttonArray) {
        button.selected = NO;
        button.backgroundColor = UIColor.whiteColor;
    }
    sender.selected = YES;
    sender.backgroundColor = [OE_LIMITE_COLOR colorWithAlphaComponent:0.5];
}
- (void)onSelctedPayButton{
    NSString * money = [self currentSelctedItemMoney];
    if (!money.length) {
        [SVProgressHUD showInfoWithStatus:OESTRING(@"Please input or chose money")];
        return;
    }
    NSString * pay_id;
    for (STTableEasyModel * model in self.dataSouce) {
        if (model.isSlected) {
            pay_id = model.obj_id;
        }
    }
    
    [OEUserDataController sendPayRequestWithMoney:money pay_id:pay_id handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:OESTRING(@"Pay Success")];
            OEPaySuccessViewController * vc =  OEPaySuccessViewController.new;
            vc.recharge_id = [resp[@"recharge_id"] description];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }];
}
@end
